<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              <el-input v-model="ruleForm.orderNo" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker v-model="ruleForm.orderDate" type="date" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd" value-format="yyyy-MM-dd" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="客户" prop="customerId">
              <el-select v-model="ruleForm.customerId" style="width: 100%" filterable>
                <el-option v-for="item in customerList" :key="item.id" :label="item.customerName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料" prop="materialId">
              <el-select v-model="ruleForm.materialId" filterable style="width: 100%" @change="ruleForm.unit = materialList.find(item => item.id === ruleForm.materialId).unit">
                <el-option v-for="item in materialList" :key="item.id" :label="`${item.materialName} ${item.unit ? '【' + item.unit + '】' : ''}`" :value="item.id">
                  <span style="float: left">{{ item.materialName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.unit }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="订货数量" prop="placeAnOrder">
              <el-input-number v-model="ruleForm.placeAnOrder" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已发数量" prop="sent">
              <el-input-number v-model="ruleForm.sent" style="width: 100%" :precision="0" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="订单简述" prop="orderDescription">
              <el-input type="textarea" rows="2" v-model="ruleForm.orderDescription" maxlength="200" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="销售跟进" prop="salesFollowUp">
              <el-input v-model="ruleForm.salesFollowUp" maxlength="200" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单交期" prop="orderDeliveryTime">
              <el-date-picker v-model="ruleForm.orderDeliveryTime" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="操作人" prop="userId">
              <el-select v-model="ruleForm.userId" style="width: 100%" filterable>
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间" prop="operatorTime">
              <el-date-picker v-model="ruleForm.operatorTime" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!--     <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
import { getCustomerListApi } from '@/api/public'
export default {
  props: {
    materialList: {
      type: Array,
      default: () => {
        return {}
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      title: '',
      ruleForm: {
        orderNo: '',
        orderDate: '',
        customerId: '',
        materialId: '',
        placeAnOrder: '',
        sent: '',
        readyToGo: '',
        orderDescription: '',
        salesFollowUp: '',
        orderDeliveryTime: '',
        userId: '',
        operatorTime: ''
      },
      rules: {
        orderNo: [{ required: true, message: '订单号不能为空！', trigger: 'blur' }],
        materialId: [{ required: true, message: '物料不能为空！', trigger: 'change' }]
      },
      dialogVisible: false,
      customerList: []
    }
  },
  watch: {},
  mounted() {
    this.getAllCustomers()
  },
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
      console.log(this.ruleForm)
      this.dialogVisible = true
    },
    /**
     * @description: 获取所有客户
     * @return {*}
     */
    async getAllCustomers() {
      const res = await getCustomerListApi()
      if (res?.data.code === 200) {
        this.customerList = res.data.data
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await this.$axios.post('/wms/statistics/out/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = Object.assign({}, this.$options.data.call(this).ruleForm)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
